package com.wandian.weather.mvvm.moredayweather.pagefragment;

import android.app.Application;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.mylib.libcore.mvvm.BaseViewModel;
import com.mylib.libcore.utils.JsonUtil;
import com.mylib.libcore.utils.RetrofitUtil;
import com.mylib.libcore.utils.UIUtils;
import com.wandian.weather.bean.TianqiWeather30Bean;
import com.wandian.weather.data.BaseAppApi;
import com.wandian.weather.tools.JmTools;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * @author Administrator
 * @date 2019/12/16-13:32
 * @description ChildPageViewModel
 **/
public class ChildPageViewModel extends BaseViewModel {
    private Retrofit retrofit;
    private  BaseAppApi request;
    public MutableLiveData<TianqiWeather30Bean> tianqiWeather30Bean = new MutableLiveData<>();
    public ChildPageViewModel(@NonNull Application application) {
        super(application);
    }
    //30天天气
    public void getWeatherData(String cityID){
        //创建Retrofit对象
        retrofit = RetrofitUtil.createWeatherRetrofit();
        //创建网络请求接口实例
        request = retrofit.create(BaseAppApi.class);
        HashMap<String, String> hashMap = new HashMap<>();
        //其他参数
        hashMap.put("cityid",cityID);
        //公用参数
        hashMap.put("os","1");
        hashMap.put("pid","4");
        hashMap.put("version","6.5.4");

        Call<Object> call = request.getThirtyDayWeatherData(JmTools.encryptParams(hashMap, "Tianqi.Weatherthr"));
        //发送网络请求(异步)
        call.enqueue(new Callback<Object>() {
            @Override
            public void onResponse(Call<Object> call, Response<Object> response) {

                if(UIUtils.isEmpty(response.body())){
                    return;
                }
                String jsonn = JsonUtil.toJson(response.body());
                Log.d("getThirtyDayWeatherData", "getThirtyDayWeatherData="+ JsonUtil.toJson(response.body()));
                try {
                    JSONObject jsonObject = new JSONObject(JsonUtil.toJson(response.body()));
                    TianqiWeather30Bean weather30Bean =(TianqiWeather30Bean) JsonUtil.parseJsonToBean(jsonObject.optString("data"), TianqiWeather30Bean.class);
                    if(!UIUtils.isEmpty(weather30Bean)){
                        Log.d("getdddddd ", "1111111="+ jsonn);
                        tianqiWeather30Bean.setValue(weather30Bean);
                    }else {

                        Log.d("getdddddd", "为空=");
                    }
                } catch (JSONException e) {
                    Log.d("getThirtyDa ", "异常=");
                    e.printStackTrace();
                }


            }

            @Override
            public void onFailure(Call<Object> call, Throwable t) {
                Log.d("getWeatherData", "error="+t.getMessage());
            }
        });
    }
}

